import store from '../store'
// 公共函数js

// 消息提示框(h5原生)
export function showToast(title = '标题', icon = 'none', bg = 'rgba(248,248,248)', position = 'center') {
	// #ifdef APP-PLUS
	plus.nativeUI.toast(title, {
		duration: 'short',
		align: 'center',
		background: bg,
		verticalAlign: position,
		type: 'text'
	})
	// #endif
	// #ifdef MP-WEIXIN
	uni.showToast({
		title,
		icon,
	})
	// #endif
}

// 检测是否为空字符串
export function isEmptyStr(str) {
	if (Object.prototype.toString.call(str) == '[object Object]') {
		for (let i in str) {
			if (str[i] == "" || str[i].trim() == "") {}
			return true;
		}
	} else if (Object.prototype.toString.call(str) == '[object String]') {
		if (str == "" || str.trim() == "")
			return true;
	}
	return false;
}

// 跳转
export function navigate(page, type = 'page', params, success) {
	let url = type === 'switchTab' ? '../' + page + '/' + page : '../' + page + '/' + page + concatUrl(params);
	console.log(url);
	if (type === 'switchTab') {
		if (params && !!params) {
			uni.setStorageSync('STORAGE_DATA', params)
		}
		console.log(2);
		uni.switchTab({
			url,
			success,
			fail: (err) => {
				console.log(err);
			}
		})
	} else {
		uni.navigateTo({
			url,
			success,
			fail: (err) => {
				console.log(err);
			}
		});
	}

}

// 日期格式化
export function dateStr(year, month) {
	let flag = false;
	if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
		flag = true;
	}
	switch (month) {
		case 1:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 2:
			if (flag) {
				return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/29';
			} else {
				return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/28';
			}
			break;
		case 3:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 4:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/30';
			break;
		case 5:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 6:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/30';
			break;
		case 7:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 8:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 9:
			return year + '/' + '0' + month + '/01-' + year + '/' + '0' + month + '/30';
			break;
		case 10:
			return year + '/' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
		case 11:
			return year + '/' + month + '/01-' + year + '/' + '0' + month + '/30';
			break;
		case 12:
			return year + '/' + month + '/01-' + year + '/' + '0' + month + '/31';
			break;
	}
}

/* 
 * cond => Object
 * cond.date => yyyy/mm/dd-yyyy/mm/dd
 */
export function concatUrl(cond) {
	let condType = Object.prototype.toString.call(cond);
	if (condType !== '[object Object]') return '';
	let keyArr = Object.keys(cond),
		concatStr = '';
	if (keyArr.length === 0) return;
	for (let i = 0; i < keyArr.length; i++) {
		concatStr += (i == 0 ? '?' : '') + keyArr[i] + '=' + cond[keyArr[i]] + (i == keyArr.length - 1 ? '' : '&');
	}
	return concatStr;
}

export function jsonParse(data) {
	var cache = [];
	var result = JSON.parse(JSON.stringify(data, function(key, value) {
		if (typeof value === 'object' && value !== null) {
			if (cache.indexOf(value) !== -1) {
				return;
			}
			cache.push(value);
		}
		return value;
	}));
	cache = null;
	return result;
}

export function toLogin() {
	// let pages = getCurrentPages();
	// let result = jsonParse(pages);
	// if(result[0].route !== 'pages/login/login') {
	// 	store.dispatch('LOGOUT');
	// 	uni.navigateTo({
	// 		url: '/pages/login/login'
	// 	});
	// 	showToast('请登录');
	// } else {
	// 	store.dispatch('LOGOUT');
	// }
		store.dispatch('LOGOUT');
		uni.navigateTo({
			url: '/pages/login/login',
			success: () => {
				showToast('请登录');
			}
		});
}